package com.lms.service.apply;

import com.lms.bean.LabData;
import com.lms.bean.UserApply;
import com.lms.dao.BaseDao;
import com.lms.dao.apply.ApplyDao;
import com.lms.dao.apply.ApplyDaoImpl;
import com.lms.pojo.UserApplyData;
import com.lms.service.lab.LabService;
import com.lms.service.lab.LabServiceImpl;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class ApplyServiceImpl implements ApplyService{
    private ApplyDao applyDao=new ApplyDaoImpl();
    private Connection connection;
    @Override
    public List<UserApplyData> getUserApplyDataList(String userCode) {
        List<UserApplyData> userApplyDataList=new ArrayList<>();
        LabService labService = new LabServiceImpl();

        /*获取UserApplyList*/
        List<UserApply> userApplyList = this.getUserApplyList(userCode);

        /*将获取到的UserApply转换为UserApplyData*/
        for (UserApply item:userApplyList){
            UserApplyData userApplyData = new UserApplyData();
            LabData labData = labService.getLabDataByLabCode(item.getLabCode());
            userApplyData.setApplyID(item.getApplyID());
            userApplyData.setLib(labData.getLabName());
            if (labData.getLabTag()==0){
                userApplyData.set_class("普通");
            }else {
                userApplyData.set_class("专业");
            }
            userApplyData.setNum(item.getApply_applyCount());
            String time="";
            switch (item.getApply_time_week()){
                case "mon":time+="周一";break;
                case "tue":time+="周二";break;
                case "wed":time+="周三";break;
                case "thur":time+="周四";break;
                case "fri":time+="周五";break;
                case "sat":time+="周六";break;
                case "sun":time+="周天";break;
            }
            switch (item.getApply_time_interval()){
                case "one":time+="1-2";break;
                case "two":time+="3-4";break;
                case "three":time+="5-6";break;
                case "four":time+="7-8";break;
                case "five":time+="9-10";break;
            }
            userApplyData.setTime(time);
            switch (item.getApply_state()){
                case 1:userApplyData.setStatus("申请成功");break;
                case 2:userApplyData.setStatus("申请失败");break;
                case 3:userApplyData.setStatus("正在审核");break;
                case 4:userApplyData.setStatus("已取消");break;
                case 5:userApplyData.setStatus("已过期");break;
            }
            String apply_time = new SimpleDateFormat("yyyy.MM.dd").format(item.getApply_creationDate());
            userApplyData.setApply_time(apply_time);
            if (item.getApply_approvalDate()!=null){
                String check_time = new SimpleDateFormat("yyyy.MM.dd").format(item.getApply_approvalDate());
                userApplyData.setCheck_time(check_time);
            }
            userApplyData.setApply_reason(item.getApply_reason());

            userApplyDataList.add(userApplyData);
        }

        return userApplyDataList;
    }

    @Override
    public List<UserApply> getUserApplyList(String userCode) {
        try {
            connection= BaseDao.getConnection();
            return applyDao.getUserApplyList(connection,userCode);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int apply(String userCode,String labCode, String applyCount, String reason, String week, String interval) {
        connection=BaseDao.getConnection();
        try {
            return applyDao.apply(connection,userCode,labCode,applyCount,reason,week,interval);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int cancelApply(String applyID) {
        connection=BaseDao.getConnection();
        try {
            return applyDao.cancelApply(connection,applyID);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int getApplyCount() {
        connection=BaseDao.getConnection();
        try {
            return applyDao.getApplyCount(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int getCheckCount(String userCode) {
        connection=BaseDao.getConnection();
        try {
            return applyDao.getCheckCount(connection,userCode);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return 0;
    }

    @Override
    public boolean checked(String userCode) {
        connection=BaseDao.getConnection();
        try {
            return applyDao.check(connection,userCode);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return false;
    }
}
